//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "adminActions\main.sqf"
#include <functions\core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

if (isServer) then {
	// MSO_AdminActions_EventHandler = [];
	// publicVariable "MSO_AdminActions_EventHandler";
	if (isNil QGVAR(OnPlayerConnectedEventId)) then { GVAR(OnPlayerConnectedEventId) = ["MSO_AA_ID", "onPlayerConnected", {[_id,_name,_uid] call FUNC(opc);}] call BIS_fnc_addStackedEventHandler; };
	if (isNil QGVAR(OnPlayerDisconnectedEventId)) then { GVAR(OnPlayerDisconnectedEventId) = ["MSO_AA_ID", "onPlayerDisconnected", {[_id,_name,_uid] call FUNC(opd);}] call BIS_fnc_addStackedEventHandler; };
	TRACE_2("Added onPlayer Events",GVAR(OnPlayerConnectedEventId),GVAR(OnPlayerDisconnectedEventId));
	
	//Update players already connected
	{ if (isPlayer _x) then { [0, name _x, getPlayerUID _x] call FUNC(opc); };
	} forEach playableUnits;
};

// Registering the event namespace
[QGVAR(PVEH)] call IFUNC(register);

// Registering all the event handlers
[QGVAR(PVEH),QGVAR(GHOSTMODE),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
[QGVAR(PVEH),QGVAR(TELEPORTTOBASE),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
[QGVAR(PVEH),QGVAR(DESTROY),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
[QGVAR(PVEH),QGVAR(BAN),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
[QGVAR(PVEH),QGVAR(KICK),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
[QGVAR(PVEH),QGVAR(JAIL),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
[QGVAR(PVEH),QGVAR(UNJAIL),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
[QGVAR(PVEH),QGVAR(REPAIR),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
[QGVAR(PVEH),QGVAR(TELEPORTTOME),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
if (isServer) then {
	[QGVAR(PVEH),QGVAR(LOG),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
};
if (!isDedicated && hasInterface) then {
	[QGVAR(PVEH),QGVAR(INFORM),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
	[QGVAR(PVEH),QGVAR(SWITCHMOVE),QUOTE(FUNC(pveh))] call IFUNC(handlerAdd);
};

//"MSO_AdminActions_EventHandler" addPublicVariableEventHandler {(_this select 1) call FUNC(pveh);};

if (GVAR(AdminCmds) == 0) exitwith {diag_log format ["MSO-%1 Adminactions turned off - Exiting.",time];};
diag_log format ["MSO-%1 Adminactions turned on",time];
"Admin Actions" call mso_core_fnc_initStat;

if (markerType "MSO_PUNISHMENT" == "") then {
	diag_log format ["MSO-%1 Adminactions missing MSO_Punishment marker.",time];
	_mrk = createMarkerLocal ["MSO_PUNISHMENT", [0,0,0]]; 
};

if (isDedicated || !hasInterface) exitWith { };

if (!isnil "tyr_fnc_addHandler") then {
	if (isNil "TYR_MENU_IsActive" || {TYR_MENU_IsActive <= 0}) then {
		["DOM_ACTION_MENU",FUNC(createdomactionmenu)] call tyr_fnc_addhandler;
	} else {
		if (GVAR(AdminCmds) == 1) then {
			["TYRMENU",[
				[
					"Moderation"
					,{((GVAR(AdminModeration) == 1 && {isNil "mp_rights_fnc_mustHaveRoles" || {[(getPlayerUID player),["moderator"]] call mp_rights_fnc_mustHaveRoles}}) || ([] call mso_core_fnc_isAdmin))}
					,{[player] spawn FUNC(moderation);}
					,"tmf\core\modules\adminActions\moderation.paa"
					,50
				],
				[
					"Ghost Mode"
					,{((GVAR(AdminCmdGhost) == 1 && {isNil "mp_rights_fnc_mustHaveRoles" || {[(getPlayerUID player),["dev"]] call mp_rights_fnc_mustHaveRoles}}) || ([] call mso_core_fnc_isAdmin))}
					,{[] call FUNC(ghostMode);}
					,"tmf\core\modules\adminActions\ghostmode.paa"
				],
				[
					"Teleport Mode"
					,{((GVAR(AdminCmdTeleport) == 1 && {isNil "mp_rights_fnc_mustHaveRoles" || {[(getPlayerUID player),["dev"]] call mp_rights_fnc_mustHaveRoles}}) || ([] call mso_core_fnc_isAdmin))}
					,{[] call FUNC(teleportMode);}
					,"tmf\core\modules\adminActions\teleport.paa"
				],
				[
					"Marker Mode"
					,{((GVAR(AdminCmdMarker) == 1 && {isNil "mp_rights_fnc_mustHaveRoles" || {[(getPlayerUID player),["dev","member"]] call mp_rights_fnc_mustHaveRoles}}) || ([] call mso_core_fnc_isAdmin))}
					,{[] call FUNC(markerMode);}
					,"tmf\core\modules\adminActions\markermode.paa"
				]
			]] call tyr_fnc_addHandler;
		};
	};
};
_trigger = createTrigger ["EmptyDetector" ,markerPos "MSO_PUNISHMENT"];
_trigger setTriggerArea [12, 12, (markerDir "MSO_PUNISHMENT"), true];
_trigger setTriggerActivation [str(side player), "PRESENT", true];
_trigger setTriggerStatements ["(player distance (markerPos 'MSO_PUNISHMENT')) < 12", "[player] spawn FNC_PUNISHMENT_PLAYER_EXEC;", ""];

if (GVAR(AdminCmds) == 1) then {
	0 spawn {
		waituntil {!isnull player};
		TRACE_1("Starting Player Functions");
		player spawn FUNC(playerMonitor);
		[] spawn FUNC(markerDisplay);
	};
};

TRACE("End");
TMFPROFILERSTOP;